﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace User_subsystem
{
    public partial class FormUpdatePwdUser : Form
    {
        public FormUpdatePwdUser()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //需要返回的可能
            if (txtNewPwd.Text == "" || txtOKPwd.Text == "" || txtOldPwd.Text == "")
            {
                //有空项
                MessageBox.Show("有空项！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if ((txtNewPwd.Text.Length > 20) || (txtNewPwd.Text.Length < 6))
            {
                //密码不足6位或者超过20位
                MessageBox.Show("密码格式不正确，密码长度为6~20位", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtNewPwd.Text != txtOKPwd.Text)
            {
                //确认密码不匹配
                MessageBox.Show("确认密码不匹配！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //原密码与数据库中做比较
            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            //读取数据库中的原密码
            string sql = $"select [Password]  from UserInfor  where [UserID] ='{FormLogin.id}'";
            SqlDataReader reader = dao.read(sql);
            reader.Read();

            string x = reader[0].ToString();
            if (!(x.Equals(txtOldPwd.Text)))
            {
                //原密码不匹配
                MessageBox.Show("原密码不匹配！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                //修改
                sql = $"update [UserInfor] set Password ='{txtNewPwd.Text}' where [UserID] ='{FormLogin.id}' ";
                if (dao.Execute(sql) > 0)
                {
                    // 修改成功
                    MessageBox.Show("修改成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    reader.Close();
                    dao.DaoClose();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
  
            }
        }

        private void FormUpdatePwdUser_Load(object sender, EventArgs e)
        {

        }
    }
}
